const AJAX = getApp().ajax,
  $wx = getApp().$wx,
  getColor = getApp().globalData.getColor;

Page({
  data: {
    getColor: {},
    showBtnStatus: 1,
    showAuthorize: false
  },

  onLoad(options) {
    let _this = this;
    
    if (options.scene) {
      wx.setStorageSync('sceneInfo', {
        key: options.scene
      });
      _this.getShopConfig();
    } else {
      let scene_info = wx.getStorageSync('sceneInfo');
      if (!scene_info) {

        AJAX.isScanCode((status, data) => {
          wx.setStorageSync('isScanCode', data.is_true);
          if (data.is_true == 1) {
            wx.showModal({
              title: '提示',
              content: '请先扫码',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  wx.scanCode({
                    success(res) {
                      let url_params = $wx.getUrlParams(res.path);

                      if (url_params) {
                        wx.setStorageSync('sceneInfo', { key: url_params.scene });
                        _this.getShopConfig();
                      } else {
                        $wx.showToast('识别失败！', setTimeout(callback => {
                          _this.onLoad({});
                        }, 1500));
                      }
                    },
                    fail(error) {
                      if (error.errMsg == 'scanCode:fail') {
                        $wx.showToast('识别失败！', setTimeout(callback =>{
                          _this.onLoad({}); 
                        }, 1500));
                      }else{
                        _this.onLoad({});
                      }
                     
                    }
                  });
                }
              }
            });
          } else {
            wx.setStorageSync('sceneInfo', { key: 'cf60c76d0d240076e5a6b436b76ddb61' });
            _this.getShopConfig();
          }
        });

      } else {
        _this.getShopConfig();
      }
    }
  },

  //获取用户信息
  getUserInfo(e) {
    let _this = this, user_info = e.detail;
    _this.isAuthorization('userInfo', (status) => {
      if (status) {
        wx.setStorageSync('userInfo', user_info);
        _this.login(user_info.userInfo);
      } else {
        $wx.showToast('请选打开授权！');

        _this.setData({
          showBtnStatus: 2
        });
      }
    });

  },

  //打开授权 
  openSetting(e) {
    let _this = this;
    _this.isAuthorization('userInfo', (status) => {
      if (status) {
        _this.setData({
          showBtnStatus: 1
        });
      }
    });
  },

  /**
   * 是否
   * */
  isScanCode() {

  },

  /**
   * 登录
   * @param code
   * @param user_nickname
   * @param avatar
   * */
  login(userInfo) {
    let _this = this;
    wx.login({
      success: loginRes => {
        wx.showLoading({
          title: '登录中...',
          mask: true
        });

        AJAX.login({
          code: loginRes.code,
          user_nickname: userInfo.nickName,
          avatar: userInfo.avatarUrl
        }, (status, data, msg) => {
          wx.hideLoading();
          if (status) {
            wx.setStorageSync('token', data.token);

            wx.redirectTo({
              url: '/pages/tab-bar/shop/shop'
            });
          } else {
            $wx.showToast(msg);
          }
        });
      }
    });
  },

  /*商店配置*/
  getShopConfig() {
    let _this = this, token = wx.getStorageSync('token');

    wx.showNavigationBarLoading();
    AJAX.getShopConfig({}, (status, data, msg) => {
      wx.hideNavigationBarLoading();

      if (status) {
        wx.setNavigationBarTitle({ title: data.shop_name });
        wx.setStorageSync('shopConfig', data);
        console.log(data.color);
        $wx.setNavColor(data.color);

        setTimeout(callback => {
          wx.redirectTo({ url: '/pages/tab-bar/shop/shop' });
        }, 2000);
        
      } else {
        wx.showModal({
          title: '提示',
          content: `${msg},是否重新载入`,
          success(res) {
            if (res.confirm) {
             wx.clearStorage({
               success(){
                _this.onLoad({});
               }
             });
            }
          }
        });
      }
    });
  },

  /*是否授权*/
  isAuthorization(type, callback) {
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.' + type]) {
          callback(true);
        } else {
          callback(false);
        }
      }
    })
  }
});